﻿using Domain.IServices;
using Domain.Models;
using Infrastructure.IService;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace Domain.Services
{
    public class GitLibraryService : IDomainService<GitLibaray>
    {
        private readonly IRepository<GitLibaray> repository;

        public GitLibraryService(IRepository<GitLibaray> repository)
        {
            this.repository = repository ?? throw new ArgumentNullException(nameof(repository));
        }

        public async Task<GitLibaray> AddAsync(GitLibaray entity, CancellationToken cancellationToken = default)
        {
            return await repository.AddAsync(entity, cancellationToken);
        }

        public async Task AddSyncLog(GitLibaray entity, string message, CancellationToken cancellationToken = default)
        {
            entity.CreateSyncLog(message);
            await repository.ModifyAsync(entity, cancellationToken);
        }

        public async Task<GitLibaray> GetEntityById(int id, CancellationToken cancellationToken = default)
        {
            return await repository.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
        }

        public async Task ModifyAsync(GitLibaray entity, CancellationToken cancellationToken = default)
        {
            await repository.ModifyAsync(entity, cancellationToken);
        }

        public async Task RemoveAsync(GitLibaray entity, CancellationToken cancellationToken = default)
        {
            await repository.RemoveAsync(entity, cancellationToken);
        }

        public async Task<IReadOnlyCollection<GitLibaray>> ToListAsync(Expression<Func<GitLibaray, bool>> predicate = null, CancellationToken cancellationToken = default)
        {
            return await repository.ToListAsync(predicate, cancellationToken);
        }

        public async Task<IReadOnlyCollection<GitLibaray>> ToListPageAsync(Expression<Func<GitLibaray, bool>> predicate = null, int size = 10, int index = 1, CancellationToken cancellationToken = default)
        {
            return await repository.ToListPageAsync(predicate, size, index, cancellationToken);
        }
    }
}
